using System;
using System.Collections.Generic;
using MagicApp.Data;

namespace MagicApp.Services
{
	public class DeckEngine: IDeckEngine
    {
        private ICardRepository _repository;
        private Deck currentDeck;
        private Hand currentHand;
        public DeckEngine(ICardRepository _repository)
        {
            this._repository = _repository;
            this.currentDeck = new Deck();
            this.currentHand = new Hand();
          
        }
        public void CreateNewDeck(string deckName)
        {
        	//TODO : make this more lightweight, there are two checks going on here.
        	if(DeckExists(deckName))
        	{
        		currentDeck = GetDeck(deckName);
        	}
        	else
        	{
        		currentDeck.DeckName = deckName;
        		this.PopulateDeck();
        	}
        	
        }
      
         public void CreateNewDeck(string deckName, string defaultDeck)
        {
        	//TODO : make this more lightweight, there are two checks going on here.
        	if(DeckExists(deckName))
        	{
        		currentDeck = GetDeck(deckName);
        	}
        	else
        	{
        		currentDeck.DeckName = deckName;
        		PopulateDeck(defaultDeck);
        	}
        	
        }
         public void PopulateDeck()
         {
         	IList<Card> deck = _repository.RetrieveDeck();
         	if(deck!=null)
         	{
         	    for(int i=1;i<deck.Count;i++)
         		    this.currentDeck.Add(deck[i]);
         	}
         }
         public void PopulateDeck(string defaultDeck)
         {
         	
         }
        public bool SaveCurrentDeck()
        {
        	return _repository.SaveDeck(currentDeck);

        }
        public void AddToCurrentDeck(Card card)
        {
        	currentDeck.Add(card);
        }
        public void DeleteFromCurrentDeck(Card card, int quantity)
        {
        	for(int i=0;i<=quantity;i++)
        		DeleteFromCurrentDeck(card);
        }
        public void DeleteFromCurrentDeck(Card card)
        {
        	currentDeck.Remove(card);
        }
        public Deck GetCurrentlyEditedDeck()
        {
        	return this.currentDeck;
        }
        public Hand GetCurrentHand()
        {
        	return this.currentHand;
        }
        public void DeleteCurrentDeck()
        {
        	currentDeck.Clear();
        }
        public bool DeckExists(string deckName)
        {
        	Deck checkDeck = GetDeck(deckName);
        	return checkDeck != null ? true : false;
        }
        public Deck GetDeck(string deckName)
        {
            IList<Card> deck = _repository.GetCards();
            currentDeck.DeckName = deckName.ToLower();
          
            foreach (Card card in deck)
            {
                if (card.DeckName == deckName.ToLower())
                    currentDeck.Add(card);
            }
           
            return currentDeck.GetNumberOfCardsInDeck() > 1 ? currentDeck : null; 
        }
        public Deck GetDeck(int deckID)
        {
            return null;
        }
        public Deck GetDeck(DateTime creationDate)
        {
            return null;
        }
        public Card DrawFromCurrentDeck()
        {
        	return currentDeck.ReturnCardInDeck(currentDeck.GetNumberOfCardsInDeck()-1);
        	
        }
		public void PopulateHand()
		{
			//TODO: Make a globals class that calls into a config file to retrieve 
			//this hardcoded hand size.
			
			for(int i=0;i<7;i++)
			{
				
				this.currentHand.Add(DrawFromCurrentDeck());
			}
		}
		
		
		
    }
}
